package com.hqs.listener.helper;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.hqs.common.helper.qprogress.QProgress;
import com.hqs.common.utils.AppUtils;
import com.hqs.common.utils.Log;
import com.hqs.common.utils.MD5Util;
import com.hqs.common.utils.SharedPreferenceUtil;
import com.hqs.listener.R;

import java.lang.ref.WeakReference;
import java.util.UUID;

/**
 * Created by apple on 16/9/27.
 */

public class Helper {

    private static Handler handler;
    private static WeakReference<QProgress> qProgress;

    public static void showProgress(Activity activity) {
        if (qProgress == null || qProgress.get() == null){
            QProgress progress = new QProgress.Builder(activity)
                    .setCancelable(true)
                    .setWheelColor(activity.getResources().getColor(R.color.colorAccent))
                    .setDismissOnTouch(false)
                    .create();
            progress.show();
            qProgress = new WeakReference<>(progress);
        }
        else{
            qProgress.get().show();
        }
    }

    public static void dismissProgress() {
        doInMainThreadDelayed(new Runnable() {
                @Override
                public void run() {
                    if (qProgress != null && qProgress.get() != null) {
                        qProgress.get().dismiss();
                        qProgress = null;
                    }
                }
        }, 200);
    }


    public static void doInBackground(Runnable r) {
        new Thread(r).start();
    }

    public static void doInMainThread(Runnable r) {
        doInMainThreadDelayed(r, 0);
    }

    public static void doInMainThreadDelayed(Runnable r, long timeInterval) {
        if (handler == null) {
            Log.print("请先初始化 handler !!!");
        } else {
            handler.postDelayed(r, timeInterval);
        }
    }

    public static void setHandler(Handler handler) {
        Helper.handler = handler;
    }

    public static void hideSoftInputFromWindow(Context context, ViewGroup view) {

        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (view != null) {
            int count = view.getChildCount();
            for (int i = 0; i < count; i++) {
                View child = view.getChildAt(i);
                if (child instanceof EditText) {
                    imm.hideSoftInputFromWindow(child.getWindowToken(), 0);
                } else {
                    if (child instanceof ViewGroup) {
                        hideSoftInputFromWindow(context, (ViewGroup) child);
                    }
                }
            }
        }
    }

    public static String encryptPassword(String pwd) {
        return MD5Util.MD5(MD5Util.MD5(pwd));
    }

    // 是否用户第一次使用
    public static boolean isFirstLaunch() {
        boolean isFirst = SharedPreferenceUtil.get("isFirstLaunch", true);
        return isFirst;
    }

    public static ColorStateList createColorList(int normal) {
        return createColorList(normal, normal);
    }

    public static ColorStateList createColorList(int normal, int pressed) {
        return createColorList(normal, pressed, pressed, pressed);
    }

    public static ColorStateList createColorList(int normal, int pressed, int focused, int unable) {
        int[] colors = new int[]{pressed, focused, normal, focused, unable, focused};
        int[][] states = new int[6][];
        states[0] = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled};
        states[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused};
        states[2] = new int[]{android.R.attr.state_enabled};
        states[3] = new int[]{android.R.attr.state_focused};
        states[4] = new int[]{android.R.attr.state_window_focused};
        states[5] = new int[]{android.R.attr.state_selected};
        ColorStateList colorList = new ColorStateList(states, colors);
        return colorList;
    }

    public static String uuid(){
        return UUID.randomUUID().toString();
    }


    public static String getDeviceInfo(){
        String info = "系统版本: " + AppUtils.getBuildVersion() + "  ";
        info += "手机型号: " + AppUtils.getPhoneBrand() + " " + AppUtils.getPhoneModel();

        return info;
    }

}
